﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Text;

namespace Web.UserControls
{
    public partial class SinglewellFilter : System.Web.UI.UserControl
    {
        private const string archiveSourceKeyName = "xxxx_archiveSourceTable";
        private const string archiveFilteredKeyName = "xxxx_archiveFilteredTable";

        private string _filterSql;
        /// <summary>
        /// 过滤SQL，不包含where关键字
        /// </summary>
        public string FilterSql
        {
            get
            {
                if (string.IsNullOrEmpty(_filterSql))
                    _filterSql = CreateFilterSql(SinglewellFilter.fieldNameList);
                return _filterSql;
            }
        }

        public string CreateFilterSql(List<string> fieldNameList)
        {
            StringBuilder filterBuilder = new StringBuilder();
            filterBuilder.Append("1=1");
            
            /*
             * 时间：2013年3月10日 21:26:35
             * 单井不需要档号和全宗号
             */

            //开工日期
            if (!string.IsNullOrEmpty(this.txtKGRQ.Text.Trim()) && fieldNameList.Contains("ARCHIVECODE"))
            {
                if (this.ddlKGRQ.SelectedValue == "1")//等于
                    filterBuilder.AppendFormat(" AND BEGINDATE=to_date('{0}','yyyy-mm-dd')   ", this.txtKGRQ.Text.Trim());
                else if (this.ddlKGRQ.SelectedValue == "2")
                    filterBuilder.AppendFormat("  AND BEGINDATE > to_date('{0}','yyyy-mm-dd')     ", this.txtKGRQ.Text.Trim());
                else
                    filterBuilder.AppendFormat("  AND BEGINDATE <to_date('{0}','yyyy-mm-dd')    ", this.txtKGRQ.Text.Trim());
            }

            //完工日期
            if (!string.IsNullOrEmpty(this.txtWGRQ.Text.Trim()) && fieldNameList.Contains("QUANZONGCODE"))
            {
                if (this.ddlWGRQ.SelectedValue == "1")//等于
                    filterBuilder.AppendFormat("  AND FINISHDATE=to_date('{0}','yyyy-mm-dd')      ", this.txtWGRQ.Text.Trim());
                else if(this.ddlKGRQ.SelectedValue == "2")
                    filterBuilder.AppendFormat("  AND FINISHDATE>to_date('{0}','yyyy-mm-dd')     ", this.txtWGRQ.Text.Trim());
                else
                    filterBuilder.AppendFormat("  AND FINISHDATE<to_date('{0}','yyyy-mm-dd')      ", this.txtWGRQ.Text.Trim());
            }
            
            //题名
            if (!string.IsNullOrEmpty(this.txtTM.Text.Trim()) && fieldNameList.Contains("DATAFILENAME"))
            {
                if (this.ddlTM.SelectedValue == "1")//等于
                    filterBuilder.AppendFormat("  AND DATAFILENAME='{0}'   ", this.txtTM.Text.Trim());
                else
                    filterBuilder.AppendFormat("  AND DATAFILENAME LIKE '%{0}%'  ", this.txtTM.Text.Trim());
            }

            //井号
            if (!string.IsNullOrEmpty(this.txtWellCode.Text.Trim()) && fieldNameList.Contains("WELLCODE"))
            {
                if (this.ddlWellCode.SelectedValue == "1")//等于
                    filterBuilder.AppendFormat("  AND WELLCODE='{0}'   ", this.txtWellCode.Text.Trim());
                else
                    filterBuilder.AppendFormat("  AND WELLCODE LIKE '%{0}%'  ", this.txtWellCode.Text.Trim());
            }

            //井别
            if (!string.IsNullOrEmpty(this.txtWellType.Text.Trim()) && fieldNameList.Contains("WELLTYPE"))
            {
                if (this.ddlWellType.SelectedValue == "1")//等于
                    filterBuilder.AppendFormat("  AND WELLTYPE='{0}'   ", this.txtWellType.Text.Trim());
                else
                    filterBuilder.AppendFormat("  AND WELLTYPE LIKE '%{0}%'  ", this.txtWellType.Text.Trim());
            }

            //施工措施
            if (!string.IsNullOrEmpty(this.txtWorkType.Text.Trim()) && fieldNameList.Contains("WORKMODE"))
            {
                if (this.ddlWorkType.SelectedValue == "1")//等于
                    filterBuilder.AppendFormat("  AND WORKMODE='{0}'   ", this.txtWorkType.Text.Trim());
                else
                    filterBuilder.AppendFormat("  AND WORKMODE LIKE '%{0}%'  ", this.txtWorkType.Text.Trim());
            }

            return filterBuilder.ToString();
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 待过滤的档案表-传入
        /// </summary>
        public DataTable ArchiveSourceTable
        {
            get
            {
                return ViewState[archiveSourceKeyName] as DataTable;
            }
            set
            {
                DataTable _archiceSourceTable = value;
                ViewState[archiveSourceKeyName] = value;
                ViewState[archiveFilteredKeyName] = value;
                foreach (DataColumn col in _archiceSourceTable.Columns)
                {
                    fieldNameList.Add(col.ColumnName.ToUpper());
                }
            }
        }

        /// <summary>
        /// 存储待过滤表的列名
        /// </summary>
        private static List<string> fieldNameList = new List<string>();


        /// <summary>
        /// 过滤后的档案表-传出
        /// </summary>
        public DataTable ArchiveFilteredTable
        {
            get
            {
                return ViewState[archiveFilteredKeyName] as DataTable;
            }
        }

        /// <summary>
        /// 过滤
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnFilter_Click(object sender, EventArgs e)
        {
            DataTable _archiceSourceTable = ViewState[archiveSourceKeyName] as DataTable;
            if (_archiceSourceTable != null && _archiceSourceTable.Rows.Count > 0)
            {
                DataTable _archiveFilteredTable = null;
                //构建过滤器
                string filterSql = CreateFilterSql(SinglewellFilter.fieldNameList);
                if (string.IsNullOrEmpty(filterSql))
                {
                    _archiveFilteredTable = _archiceSourceTable;
                }
                else
                {
                    //过滤 _archiceSourceTable，并为 _archiveFilteredTable 赋值
                    DataRow[] rows = _archiceSourceTable.Select(filterSql);
                    _archiveFilteredTable = _archiceSourceTable.Clone();
                    foreach (DataRow row in rows)
                    {
                        _archiveFilteredTable.Rows.Add(row.ItemArray);
                    }
                }
                ViewState[archiveFilteredKeyName] = _archiveFilteredTable;
            }
            //回调函数
            if (AfterFiltered != null)
                AfterFiltered(sender, e);
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            //this.txtArchiveCode.Text = string.Empty;
            //this.txtQuanzonghao.Text = string.Empty;
            this.txtTM.Text = string.Empty;
            this.txtWellCode.Text = string.Empty;
            this.txtWellType.Text = string.Empty;
            this.txtWorkType.Text = string.Empty;
            ViewState[archiveFilteredKeyName] = null;
            _filterSql = string.Empty;
            if (ClearFiltered != null)
                ClearFiltered(sender, e);
        }

        /// <summary>
        /// 过滤完成之后触发的事件-通常用于过滤结果的绑定
        /// </summary>
        public event EventHandler AfterFiltered;

        /// <summary>
        /// 点击清除按钮
        /// </summary>
        public event EventHandler ClearFiltered;
    }
}